﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Delegates
{
    public delegate void PrintStartedEventHandler();
    public class Printer
    {
        public event PrintStartedEventHandler PrintStarted;
        public event EventHandler PrintFinished;
        public event EventHandler<PrintEventArgs> Printing;
        protected virtual void OnPrintStarted()
        {
            if (PrintStarted != null)
            {
                PrintStartedEventHandler del = (PrintStartedEventHandler)PrintStarted;
                del();
            }
        }

        protected virtual void OnPrintFinished()
        {
            if (PrintStarted != null)
            {
                EventHandler del = (EventHandler)PrintFinished;
                del(this, EventArgs.Empty);
            }
        }

        protected virtual void OnPrinting(int current, int total)
        {
            if (Printing != null)
            {
                EventHandler<PrintEventArgs> del = (EventHandler<PrintEventArgs>)Printing;
                PrintEventArgs e = new PrintEventArgs() {
                    CurrentPage = current,
                    RemainingPages = total - current
                };
                del(this, e);
            }
        }
        public void Print()
        {
            Thread.Sleep(1500);
            OnPrintStarted();
            int pageCount = 3;
            for (int i = 0; i < pageCount; i++)
            {
                Thread.Sleep(1000);
                OnPrinting(i+1, pageCount);
            }
            Thread.Sleep(1000);
            OnPrintFinished();

        }


    }
}
